package br.ucb.cesbdao.janelas;

/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/*
 * JanelaCadastraPaciente.java
 *
 * Created on 20/06/2012, 14:38:15
 */

import java.sql.SQLException;

import javax.swing.ButtonGroup;

import org.apache.log4j.Logger;

import br.ucb.cesbdao.beans.Pessoa;
import br.ucb.cesbdao.enuns.EnumFormatos;
import br.ucb.cesbdao.enuns.EnumLabelAndBotao;
import br.ucb.cesbdao.enuns.EnumMensagens;
import br.ucb.cesbdao.enuns.EnumResources;
import br.ucb.cesbdao.model.PessoaModel;
import br.ucb.cesbdao.model.impl.PessoaModelImpl;
import br.ucb.cesbdao.servicos.Servicos;
import br.ucb.cesbdao.servicos.Validadores;
import br.ucb.cesbdao.visao.Visao;

/**
 *
 * @author LAB_013_02
 */
public class JanelaCadastraPaciente extends javax.swing.JFrame {
	private static final long serialVersionUID = -651258407824359873L;
	
	
	private static int idPaciente;
	static Logger logger = Servicos.carregaLog(JanelaCadastraPaciente.class);
	
	public static int getIdPaciente() {
		return idPaciente;
	}

	public void setIdPaciente(int idPaciente) {
		JanelaCadastraPaciente.idPaciente = idPaciente;
	}

	
	
	/** Creates new form JanelaCadastraPaciente */
    public JanelaCadastraPaciente() {
        initComponents();
        setResizable(false);
    }

    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    // <editor-fold defaultstate="collapsed" desc="Generated Code">
    private void initComponents() {

    	PessoaModel pessoaModel = new PessoaModelImpl();
        nomePaciente = new javax.swing.JLabel();
        txt_nomePaciente = new javax.swing.JTextField();
        erroNomePaciente = new javax.swing.JLabel();
        
        rb_masculinoPaciente = new javax.swing.JRadioButton();
        rb_femininoPaciente = new javax.swing.JRadioButton();
        sexoPaciente = new javax.swing.JLabel();
        codPaciente = new javax.swing.JLabel();
        txt_codPaciente = new javax.swing.JTextField();
        painelCadastroOptionsCadastro = new javax.swing.JPanel();
        btn_cancelarCadastrar = new javax.swing.JButton();
        btn_limparCadastrar = new javax.swing.JButton();
        btn_salvarCadastrar1 = new javax.swing.JButton();
        painelCadastroPaciente = new javax.swing.JPanel();
        painelSexoPaciente = new javax.swing.JPanel();

        

        painelCadastroPaciente.setVisible(true);
        painelCadastroPaciente.setBorder(javax.swing.BorderFactory.createTitledBorder(null, EnumLabelAndBotao.ATENDIDO.getTexto(), javax.swing.border.TitledBorder.DEFAULT_JUSTIFICATION, javax.swing.border.TitledBorder.DEFAULT_POSITION, new java.awt.Font("Comic Sans MS", 1, 14), new java.awt.Color(0, 51, 153))); // NOI18N
        painelCadastroPaciente.setMinimumSize(new java.awt.Dimension(300, 240));

        nomePaciente.setFont(new java.awt.Font(EnumFormatos.FONTE_TAHOMA.getFormato(), 1, 12));
        nomePaciente.setText(EnumLabelAndBotao.NOME.getTexto());

        txt_nomePaciente.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                txt_nomePacienteActionPerformed(evt);
            }
        });

        erroNomePaciente.setFont(new java.awt.Font(EnumFormatos.FONTE_COMIC.getFormato(), 1, 10));
        erroNomePaciente.setForeground(new java.awt.Color(255, 0, 0));

        painelSexoPaciente.setBorder(javax.swing.BorderFactory.createTitledBorder(null, "", javax.swing.border.TitledBorder.DEFAULT_JUSTIFICATION, javax.swing.border.TitledBorder.DEFAULT_POSITION, new java.awt.Font(EnumFormatos.FONTE_TAHOMA.getFormato(), 1, 11))); // NOI18N

        rb_masculinoPaciente.setSelected(true);
        rb_masculinoPaciente.setText(EnumLabelAndBotao.MASCULINO.getTexto());
        rb_masculinoPaciente.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                rb_masculinoPacienteActionPerformed(evt);
            }
        });

        rb_femininoPaciente.setText(EnumLabelAndBotao.FEMININO.getTexto());
        rb_femininoPaciente.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                rb_femininoPacienteActionPerformed(evt);
            }
        });
        //Grupo de Radio
        ButtonGroup butaoGrupo = new ButtonGroup();
		butaoGrupo.add(rb_masculinoPaciente);
		butaoGrupo.add(rb_femininoPaciente);
		
        javax.swing.GroupLayout painelSexoPacienteLayout = new javax.swing.GroupLayout(painelSexoPaciente);
        painelSexoPaciente.setLayout(painelSexoPacienteLayout);
        painelSexoPacienteLayout.setHorizontalGroup(
            painelSexoPacienteLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(painelSexoPacienteLayout.createSequentialGroup()
                .addContainerGap()
                .addComponent(rb_masculinoPaciente)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(rb_femininoPaciente)
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
        );
        painelSexoPacienteLayout.setVerticalGroup(
            painelSexoPacienteLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, painelSexoPacienteLayout.createSequentialGroup()
                .addContainerGap(8, Short.MAX_VALUE)
                .addGroup(painelSexoPacienteLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(rb_masculinoPaciente)
                    .addComponent(rb_femininoPaciente))
                .addContainerGap())
        );

        sexoPaciente.setFont(new java.awt.Font(EnumFormatos.FONTE_TAHOMA.getFormato(), 1, 12));
        sexoPaciente.setText("Sexo:");

        codPaciente.setFont(new java.awt.Font(EnumFormatos.FONTE_TAHOMA.getFormato(), 1, 12));
        codPaciente.setText("Código:");

        txt_codPaciente.setEnabled(false);
        int maiorCodigoP = pessoaModel.apresentaCodigoUnico();
		txt_codPaciente.setText(Integer.toString(maiorCodigoP + 1));
        
        painelCadastroOptionsCadastro.setBorder(javax.swing.BorderFactory.createTitledBorder(null, EnumLabelAndBotao.OPCOES.getTexto(), javax.swing.border.TitledBorder.DEFAULT_JUSTIFICATION, javax.swing.border.TitledBorder.DEFAULT_POSITION, new java.awt.Font("Comic Sans MS", 1, 14), new java.awt.Color(0, 0, 153))); // NOI18N

        btn_cancelarCadastrar.setIcon(new javax.swing.ImageIcon(getClass().getResource(EnumResources.ICONE_CANCELAR.getCaminho()))); // NOI18N
        btn_cancelarCadastrar.setText(EnumLabelAndBotao.CANCELAR.getTexto());
        btn_cancelarCadastrar.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btn_cancelarCadastrarActionPerformed(evt);
            }
        });

        btn_limparCadastrar.setIcon(new javax.swing.ImageIcon(getClass().getResource(EnumResources.ICONE_LIMPAR.getCaminho()))); // NOI18N
        btn_limparCadastrar.setText("Limpar");
        btn_limparCadastrar.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btn_limparCadastrarActionPerformed(evt);
            }
        });

        btn_salvarCadastrar1.setIcon(new javax.swing.ImageIcon(getClass().getResource(EnumResources.ICONE_SALVAR.getCaminho()))); // NOI18N
        btn_salvarCadastrar1.setText(EnumResources.ICONE_SALVAR.getCaminho());
        btn_salvarCadastrar1.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                try {
					btn_salvarCadastrar1ActionPerformed(evt);
				} catch (SQLException e) {
					logger.error(e.getMessage());
					e.printStackTrace();
				} catch (ClassNotFoundException e) {
					logger.error(e.getMessage());
					e.printStackTrace();
				}
            }
        });

        javax.swing.GroupLayout painelCadastroOptionsCadastroLayout = new javax.swing.GroupLayout(painelCadastroOptionsCadastro);
        painelCadastroOptionsCadastro.setLayout(painelCadastroOptionsCadastroLayout);
        painelCadastroOptionsCadastroLayout.setHorizontalGroup(
            painelCadastroOptionsCadastroLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(painelCadastroOptionsCadastroLayout.createSequentialGroup()
                .addContainerGap()
                .addComponent(btn_salvarCadastrar1, javax.swing.GroupLayout.DEFAULT_SIZE, 111, Short.MAX_VALUE)
                .addGap(18, 18, 18)
                .addComponent(btn_limparCadastrar, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                .addGap(18, 18, 18)
                .addComponent(btn_cancelarCadastrar)
                .addGap(35, 35, 35))
        );
        painelCadastroOptionsCadastroLayout.setVerticalGroup(
            painelCadastroOptionsCadastroLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(painelCadastroOptionsCadastroLayout.createSequentialGroup()
                .addGroup(painelCadastroOptionsCadastroLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(btn_salvarCadastrar1)
                    .addComponent(btn_limparCadastrar)
                    .addComponent(btn_cancelarCadastrar))
                .addContainerGap(21, Short.MAX_VALUE))
        );

        javax.swing.GroupLayout painelCadastroPacienteLayout = new javax.swing.GroupLayout(painelCadastroPaciente);
        painelCadastroPaciente.setLayout(painelCadastroPacienteLayout);
        painelCadastroPacienteLayout.setHorizontalGroup(
            painelCadastroPacienteLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(painelCadastroPacienteLayout.createSequentialGroup()
                .addContainerGap(28, Short.MAX_VALUE)
                .addGroup(painelCadastroPacienteLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(painelCadastroPacienteLayout.createSequentialGroup()
                        .addComponent(codPaciente)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                        .addComponent(txt_codPaciente, javax.swing.GroupLayout.PREFERRED_SIZE, 63, javax.swing.GroupLayout.PREFERRED_SIZE))
                    .addGroup(painelCadastroPacienteLayout.createSequentialGroup()
                        .addComponent(nomePaciente)
                        .addGap(18, 18, 18)
                        .addComponent(txt_nomePaciente, javax.swing.GroupLayout.PREFERRED_SIZE, 288, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(erroNomePaciente))
                    .addGroup(painelCadastroPacienteLayout.createSequentialGroup()
                        .addComponent(sexoPaciente)
                        .addGap(18, 18, 18)
                        .addComponent(painelSexoPaciente, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)))
                .addGap(100, 100, 100))
            .addGroup(painelCadastroPacienteLayout.createSequentialGroup()
                .addGap(18, 18, 18)
                .addComponent(painelCadastroOptionsCadastro, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addContainerGap(29, Short.MAX_VALUE))
        );
        painelCadastroPacienteLayout.setVerticalGroup(
            painelCadastroPacienteLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(painelCadastroPacienteLayout.createSequentialGroup()
                .addGap(21, 21, 21)
                .addGroup(painelCadastroPacienteLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(codPaciente)
                    .addComponent(txt_codPaciente, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addGap(41, 41, 41)
                .addGroup(painelCadastroPacienteLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(nomePaciente)
                    .addComponent(txt_nomePaciente, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(erroNomePaciente))
                .addGroup(painelCadastroPacienteLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(painelCadastroPacienteLayout.createSequentialGroup()
                        .addGap(49, 49, 49)
                        .addComponent(sexoPaciente))
                    .addGroup(painelCadastroPacienteLayout.createSequentialGroup()
                        .addGap(33, 33, 33)
                        .addComponent(painelSexoPaciente, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, 18, Short.MAX_VALUE)
                .addComponent(painelCadastroOptionsCadastro, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addContainerGap())
        );

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGap(0, 493, Short.MAX_VALUE)
            .addGap(0, 493, Short.MAX_VALUE)
            .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                .addGroup(layout.createSequentialGroup()
                    .addContainerGap()
                    .addComponent(painelCadastroPaciente, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                    .addContainerGap()))
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGap(0, 367, Short.MAX_VALUE)
            .addGap(0, 367, Short.MAX_VALUE)
            .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                .addGroup(layout.createSequentialGroup()
                    .addContainerGap()
                    .addComponent(painelCadastroPaciente, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)))
        );

        pack();
    }// </editor-fold>

    private void txt_nomePacienteActionPerformed(java.awt.event.ActionEvent evt) {
    }

    private void rb_masculinoPacienteActionPerformed(java.awt.event.ActionEvent evt) {
    }

    private void rb_femininoPacienteActionPerformed(java.awt.event.ActionEvent evt) {
    }

    private void btn_cancelarCadastrarActionPerformed(java.awt.event.ActionEvent evt) {
        btn_limparCadastrarActionPerformed(evt);
        setVisible(false);
    }

    private void btn_limparCadastrarActionPerformed(java.awt.event.ActionEvent evt) {
    	txt_nomePaciente.setText("");
    }

    private void btn_salvarCadastrar1ActionPerformed(java.awt.event.ActionEvent evt) throws SQLException, ClassNotFoundException {
    	PessoaModel model = new PessoaModelImpl();
        boolean erro = false;
        if (!Validadores.validaString("Nome inválido!", txt_nomePaciente, erroNomePaciente))
			erro = true;
		if (!erro){
			if(model.addPessoa(new Pessoa(txt_nomePaciente.getText().toUpperCase().trim(), rb_masculinoPaciente.isSelected() ? 'M' : 'F', 'A')) != 0){
				Visao.showMessage(EnumMensagens.MSG_CONFIRMACAO_CADASTRO_SUCESSO.getMensagem(), "Aviso do Sistema");
				int maiorCodigoP = model.apresentaCodigoUnico();
				txt_codPaciente.setText(Integer.toString(maiorCodigoP + 1));
				setIdPaciente(maiorCodigoP);
				setVisible(false);
		    }
			else
				Visao.showMessageWarning(EnumMensagens.MSG_ERRO_CADASTRO.getMensagem(), "Aviso do Sistema");
			btn_limparCadastrarActionPerformed(evt);
		}
		return;
    }
    // Variables declaration - do not modify
    private javax.swing.JButton btn_cancelarCadastrar;
    private javax.swing.JButton btn_limparCadastrar;
    private javax.swing.JButton btn_salvarCadastrar1;
    private javax.swing.JLabel codPaciente;
    private javax.swing.JLabel erroNomePaciente;
    private javax.swing.JLabel nomePaciente;
    private static javax.swing.JPanel painelCadastroOptionsCadastro;
    private static javax.swing.JPanel painelCadastroPaciente;
    private javax.swing.JPanel painelSexoPaciente;
    private static javax.swing.JRadioButton rb_femininoPaciente;
    private static javax.swing.JRadioButton rb_masculinoPaciente;
    private javax.swing.JLabel sexoPaciente;
    private javax.swing.JTextField txt_codPaciente;
    private static javax.swing.JTextField txt_nomePaciente;
    // End of variables declaration
}
